Basic libraries for dealing with data:
library(plotly) # interactive graphs
## Loading required package: ggplot2
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
library(RColorBrewer) # make new color sets
library(tidyverse) # easy manipulation of dataframes
## ── Attaching packages ────────────────────────── tidyverse 1.2.1 ──
## ✔ tibble 1.4.2 ✔ purrr 0.2.5
## ✔ tidyr 0.8.2 ✔ dplyr 0.7.8
## ✔ readr 1.3.1 ✔ stringr 1.3.1
## ✔ tibble 1.4.2 ✔ forcats 0.3.0
## ── Conflicts ───────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks plotly::filter(), stats::filter()
## ✖ dplyr::lag() masks stats::lag()
library(modelr) # easy piping stats/model functions
library(DT) # generate interactive html tables
library(rvest) # scrap web pages
## Loading required package: xml2
##
## Attaching package: 'rvest'
## The following object is masked from 'package:purrr':
##
## pluck
## The following object is masked from 'package:readr':
##
## guess_encoding
source("utils.R") # custom files with utilitary functions
Some libraries for better handling Rmarkdown:
library(DT) # iteractive datatables
despesas.funcao.2017 <- read_csv2(unz(fin.estado17.path, "finbra.csv"),
locale=locale(encoding="ISO-8859-15"),
skip=3,
col_types = cols(.default = col_factor(NULL),
"População" = col_number(),
Valor = col_number()))
datatable(despesas.funcao.2017, options=list(pageLength=5), filter="top")
datatable(estados45ano.rawtable, options=list(scrollX=T), rownames=F)
# leitura do CSV extraído do HTML
estados45ano.df <- read_csv2(join.path(data.dir, "ideb/ideb-csv/ideb-estados-45ano.csv"),
col_types = cols(Estado = col_factor(NULL),
Ano = col_factor(NULL),
Ideb = col_double(),
Meta = col_double()))
estados45ano.df <- mutate(estados45ano.df, Estado=rename.brstates(Estado))
datatable(estados45ano.df, options=list(scrollX=T), rownames=F, filter="top")
datatable(despesas.funcao.2017,
rownames=F,
filter="top",
options=list(pageLength = 5, scrollX=T))
despesas.funcao.2017 %>%
select(Conta) %>%
unique() %>%
datatable(rownames=F, filter="top")
despesas.funcao.2017 %>%
select(UF, População) %>%
unique() %>%
ggplot(aes(x=UF, y=População)) + geom_bar(stat = "identity")
É aplicado um filtro para calcular a soma total de investimentos em educação de todo tipo “código inicia com 12”
Problema: nem todos os estados têm lançamentos para “Educação Básica” e alguns não têm para “Ensino Fundamental”, então vou deixar todos com código “12” por enquanto.
# prepara tabela com "UF", "População", "Investimentos" e "Proporção"
despesas.educ.2017 <- despesas.funcao.2017 %>%
filter(startsWith(as.character(Conta), "12")) %>% # todas que começam com "12", "12.1234" etc.
## filter(startsWith(as.character(Conta), "12.361")) %>% # Ensino Fundamental
group_by(UF) %>%
summarise(População=first(População), InvEduc = sum(Valor)) %>%
mutate(PropInv = InvEduc/População)
despesas.educ.2017
## # A tibble: 27 x 4
## UF População InvEduc PropInv
## <fct> <dbl> <dbl> <dbl>
## 1 MT 3305531 15114743937. 4573.
## 2 CE 8963663 17613746314. 1965.
## 3 PI 3212180 9452850974. 2943.
## 4 RO 1787279 6570281842. 3676.
## 5 MS 2682386 10966973267. 4089.
## 6 AP 782295 5036294137. 6438.
## 7 AL 3358963 4005264736. 1192.
## 8 AC 816687 6146589064. 7526.
## 9 BA 15276566 28606575869. 1873.
## 10 MG 20997560 58586681675. 2790.
## # ... with 17 more rows
# Gráfico de barra Investimento total
ggplot(despesas.educ.2017, aes(x=UF, y=InvEduc)) +
geom_bar(stat = "identity") +
ggtitle("Despesas em Educação por Estado") +
theme(plot.title = element_text(hjust = 0.5)) +
ylab("Investimento Total")
# Gráfico de barra com proporções Investimento/População
ggplot(despesas.educ.2017, aes(x=UF, y=PropInv)) +
geom_bar(stat="identity") +
geom_smooth() +
ggtitle("Investimento em Educação Per Capta") +
ylab("Investimento em Educação Per Capta")
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
estados45ano.df %>%
filter(Ano==2017) %>%
mutate(Estado = factor(Estado)) %>%
ggplot(aes(x=Estado, y=Ideb)) + geom_bar(stat="identity")
# prepara tabela, unindo Ideb com Proporção de investimentos
estados45ano.df %>%
filter(Ano==2017) %>%
rename(UF=Estado) %>%
inner_join(despesas.educ.2017, by="UF") %>%
mutate(UF=factor(UF)) ->
estadosInv.2017
## Warning: Column `UF` joining factors with different levels, coercing to character vector
p-value <2e-16ks.test(estadosInv.2017$InvEduc, 'pnorm')
##
## One-sample Kolmogorov-Smirnov test
##
## data: estadosInv.2017$InvEduc
## D = 1, p-value <2e-16
## alternative hypothesis: two-sided
ks.test(estadosInv.2017$Ideb, 'pnorm')
## Warning in ks.test(estadosInv.2017$Ideb, "pnorm"): ties should not be present for the
## Kolmogorov-Smirnov test
##
## One-sample Kolmogorov-Smirnov test
##
## data: estadosInv.2017$Ideb
## D = 1, p-value <2e-16
## alternative hypothesis: two-sided
p-value <2e-16shapiro.test(estadosInv.2017$InvEduc)
##
## Shapiro-Wilk normality test
##
## data: estadosInv.2017$InvEduc
## W = 0.54, p-value = 4e-08
p-value = 0.2shapiro.test(estadosInv.2017$Ideb)
##
## Shapiro-Wilk normality test
##
## data: estadosInv.2017$Ideb
## W = 0.95, p-value = 0.2
ggplot(estadosInv.2017) +
geom_point(aes(x=InvEduc, y=Ideb)) +
xlab("Investimento per Capta") +
ylab("IDEB")
cor.test( ~ InvEduc + Ideb,
data=estadosInv.2017,
method = "spearman",
continuity = FALSE,
conf.level = 0.95)
## Warning in cor.test.default(x = c(6146589064.27, 4005264736.14, 5036294136.68, : Cannot compute
## exact p-value with ties
##
## Spearman's rank correlation rho
##
## data: InvEduc and Ideb
## S = 1700, p-value = 0.01
## alternative hypothesis: true rho is not equal to 0
## sample estimates:
## rho
## 0.4789
cor.test( ~ InvEduc + Ideb,
data=estadosInv.2017,
method = "pearson",
continuity = FALSE,
conf.level = 0.95)
##
## Pearson's product-moment correlation
##
## data: InvEduc and Ideb
## t = 3, df = 25, p-value = 0.007
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## 0.1614 0.7456
## sample estimates:
## cor
## 0.5101